#include <iostream>
#include "../comm/httplib.h"
#include "oj_control.hpp"
#include <signal.h>

using namespace ns_control;
using namespace httplib;
static Control* crl_ptr = nullptr;
void recovery(int signo)
{
  LOG(DEBUG)<<"信号捕捉成功"<<"\n";
    crl_ptr->recoveryMachine();
}

// oj服务功能路由
int main()
{
  // 一键上线
  signal(SIGQUIT,recovery);
  Server server;
  Control crl;
  crl_ptr = &crl;
  // 获取所有题目列表
  server.Get("/all_questions", [&crl](const Request &req, Response &resp)
             { 
                string html;
                LOG(INFO)<<"测试----------"<<"\n";
                crl.allQuestion(&html);
                resp.set_content(html, "text/html;charset=utf-8"); });
  // 根据客户题号获取对应题目
  server.Get(R"(/question/(\d+))", [&crl](const Request &req, Response &resp)
             {   std::string number = req.matches[1];
                  string html;
                  crl.question(number,&html);
                 resp.set_content(html, "text/html;charset=utf-8"); });
  // 进行线上判题服务
  server.Post(R"(/judge/(\d+))", [&crl](const Request &req, Response &resp)
              { std::string number = req.matches[1];
                string html;
                string out_json;
                crl.judge(number,req.body,&out_json);
                 resp.set_content(out_json,"application/json;charset=utf-8"); });
                 
  server.set_base_dir("./wwwroot");
  server.listen("0.0.0.0", 8888);
  return 0;
}